package icloud.com.myapplication.ui.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

/**
 * 创建人：Administrator
 * 创建时间：2016/12/2
 * 功能描述：WebView的初始化
 */

public class XWebView extends WebView {

    private ProgressBar mPb;
    private Context mContext;

    public XWebView(Context context) {
        super(context);
//        init();
        mContext=context;
    }

    public XWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
    }

    public XWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext=context;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public XWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext=context;
    }

    /***
     * 初始化WebView的一些属性
     */
    public void init() {
        WebSettings settings = getSettings();
        settings.setJavaScriptEnabled(true);//JS代码执行
        // 设置可以支持缩放
        settings.setSupportZoom(false);
        // 设置出现缩放工具
        settings.setBuiltInZoomControls(false);
        // 扩大比例的缩放
        settings.setUseWideViewPort(false);
        clearHistory();
        clearCache(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        setWebViewClient(new XWebViewClient());
        if(getPb()!=null){
            setWebChromeClient(new XChromeClient(getPb()));
        }else{
            setWebChromeClient(new XChromeClient());
        }

    }

    public ProgressBar getPb() {
        return mPb;
    }

    public void setPb(ProgressBar pb) {
        mPb = pb;
    }

    private class XWebViewClient extends WebViewClient {

        public XWebViewClient() {
            super();
        }


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }

        /***
         * 页面加载完成
         *
         * @param view
         * @param url
         */
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError
                error) {
            super.onReceivedError(view, request, error);
            Toast.makeText(mContext,"网页加载错误",Toast.LENGTH_LONG).show();
        }

        /***
         * 开始加载，初始化一些数据
         *
         * @param view
         * @param url
         * @param favicon
         */
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }
    }

    private class XChromeClient extends WebChromeClient {

        private ProgressBar mPb;

        public  XChromeClient(){

        }

        public  XChromeClient(ProgressBar pb){
            this.mPb=pb;
        }

        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            return super.onJsAlert(view, url, message, result);
        }

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            mPb.setProgress(newProgress);
            if(newProgress==100){
                mPb.setVisibility(View.GONE);
            }else{
                mPb.setVisibility(View.VISIBLE);
            }
        }
    }
}
